

kab <- readShapePoly("./_3_data/map_data/admin_maps/JavaIslandshp/JavaIslandpoly.shp")
kab@data$id <- rownames(kab@data)
kab.points <- fortify(kab)

#opium
op <- readShapePoly("./_3_data/map_data/opium_maps/opium_banned_1886.shp")

#intersection
clip <- gIntersection(op, kab, byid = TRUE, drop_lower_td = TRUE) #clip polygon 2 with polygon 1
clip.points <- fortify(clip)

## ifls villages
banned <- cluster_level
banned <- banned[banned$prov_code >30 & banned$prov_code < 40,]
banned <- banned[banned$long > 90 & banned$long < 200,]
banned <- banned[!is.na(banned$opium_ban),]
banned$opium_ban <- ifelse(banned$opium_ban == 1, "Banned", "Legal")
banned <- banned %>% filter(!(kab_code %in% c(3526, 3527, 3528, 3529))) #removing kabs in madura

#banned <- banned %>% filter(long < 112.72 & lat > -7.16)

banned <- 
  banned %>% 
  mutate(opium_highlight = case_when(abs(dist) < 10000 & opium_ban == "Banned" ~ "In-sample",
                                     abs(dist) < 10000 & opium_ban == "Legal" ~ "In-sample",
                                     TRUE ~ "Out-of-sample"),
         salt_highlight = case_when(abs(salt_dist) < 10000 & salt_banned == 1 ~ "In-sample",
                                    abs(salt_dist) < 10000 & salt_banned == 0 ~ "In-sample",
                                    TRUE ~ "Out-of-sample")
  )

p <- 
  ggplot() + 
  geom_map(data=kab.points, map=kab.points, aes(x=long, y=lat, map_id=id), fill = "white", color="black", size=0.1) + 
  coord_map() + 
  geom_map(data=clip.points, map=clip.points, aes(x=long, y=lat, map_id=id), fill = "737272", color="black", size=0.1) +
  theme_minimal() + 
  theme(axis.title = element_blank(), 
        axis.text = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank()) + 
  scale_x_continuous(limits = c(105.00 , 114.66)) + 
  scale_y_continuous(limits = c(-8.88, -6.00))


ggsave("./_4_outputs/figures/2a_map_opium.pdf", plot = p,  width = 12, height = 4)


p2 <- 
  ggplot() + 
  geom_map(data=kab.points, map=kab.points, aes(x=long, y=lat, map_id=id), fill = "white", color="black", size=0.1) + 
  coord_map() + 
  geom_map(data=clip.points, map=clip.points, aes(x=long, y=lat, map_id=id), fill = "lightgrey", color="black", size=0.1) +
  theme_minimal() + 
  theme(axis.title = element_blank(), 
        axis.text = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank()) + 
  scale_x_continuous(limits = c(105.00 , 114.66)) + 
  scale_y_continuous(limits = c(-8.88, -6.00)) + 
  geom_point(data = banned %>% filter(opium_highlight == "In-sample"), aes(x = long, y = lat, fill = opium_ban), shape = 21, color = "black") + 
  scale_fill_manual(values = c("darkgrey", "white")) +
  theme(legend.position="none")

#2B8CBE

ggsave("./_4_outputs/figures/figure_2a.tiff", plot = p2,  width = 5, height = 2, units="in", dpi=300, compression = "lzw")


#salt
salt <- readShapePoly("./_3_data/map_data/salt_maps/salt_map_no_kabs2.shp")

#intersection
clip <- gIntersection(salt, kab, byid = TRUE, drop_lower_td = TRUE) #clip polygon 2 with polygon 1
clip.points <- fortify(clip)


p3 <- 
  ggplot() + 
  geom_map(data=kab.points, map=kab.points, aes(x=long, y=lat, map_id=id), fill = "white", color="black", size=0.1) + 
  coord_map() + 
  geom_map(data=clip.points, map=clip.points, aes(x=long, y=lat, map_id=id), fill = "737272", color="black", size=0.1) +
  theme_minimal() + 
  theme(axis.title = element_blank(), 
        axis.text = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank()) + 
  scale_x_continuous(limits = c(105.00 , 114.66)) + 
  scale_y_continuous(limits = c(-8.88, -6.00))

ggsave("./_4_outputs/figures/2c_map_salt.pdf", plot = p3,  width = 12, height = 4)


p4 <- 
  ggplot() + 
  geom_map(data=kab.points, map=kab.points, aes(x=long, y=lat, map_id=id), fill = "white", color="black", size=0.1) + 
  coord_map() + 
  geom_map(data=clip.points, map=clip.points, aes(x=long, y=lat, map_id=id), fill = "lightgrey", color="black", size=0.1) +
  theme_minimal() + 
  theme(axis.title = element_blank(), 
        axis.text = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank()) + 
  
  scale_x_continuous(limits = c(105.00 , 114.66)) + 
  scale_y_continuous(limits = c(-8.88, -6.00)) + 
  geom_point(data = banned %>% filter(salt_highlight == "In-sample"), aes(x = long, y = lat, fill = factor(salt_banned)), shape = 21, color = "black") + 
  scale_fill_manual(values = c("darkgrey", "white")) +
  theme(legend.position="none")


ggsave("./_4_outputs/figures/figure_2b.tiff", plot = p4,  width = 5, height = 2, units="in", dpi=300, compression = "lzw")
